<template>
  <div class="app-bg auto">
    <div class="row app-input">
      <label class="col-xs-4 control-label text-left">户号</label>
      <input class="col-xs-8" v-model="model.f_userinfo_id" placeholder="请输入用户编号">
    </div>
    <div class="row app-input">
      <label class="col-xs-4 control-label text-left">姓名</label>
      <input class="col-xs-8" v-model="model.f_user_name" placeholder="请输入用户姓名">
    </div>
    <div class="row app-input">
      <label class="col-xs-4 control-label text-left">电话</label>
      <input class="col-xs-8" v-model="model.f_user_phone" placeholder="请输入用户电话">
    </div>
    <div class="row app-input">
      <label class="col-xs-4 control-label text-left">开始时间</label>
      <datepicker
        :value.sync="model.f_start_date"
        :disabled-days-of-week="[]"
        :format="'yyyy-MM-dd'"
        :show-rest-button="reset"
        v-model="model.f_start_date"
        condition="f_checkdate >= '{}'">
      </datepicker>
    </div>
    <div class="row app-input">
      <label class="col-xs-4 control-label text-left">结束时间</label>
      <datepicker
        :value.sync="model.f_end_date"
        :disabled-days-of-week="[]"
        :format="'yyyy-MM-dd'"
        :show-rest-button="reset"
        v-model="model.f_end_date"
        condition="f_checkdate >= '{}'">
      </datepicker>
    </div>
    <div class="app-btn">
    <button type="button" name="button" class="btn btn-primary"  @click="$back()">
      <span  class="glyphicon glyphicon-remove-circle" style="margin-right: 15px;"></span>返回
    </button>
    <button type="button" name="button" class="btn btn-primary"  @click="search()">
      <span  class="glyphicon glyphicon-zoom-in" style="margin-right: 15px;"></span>查询
    </button>
  </div>
  <div v-for='row in users' class="app-list" >
    <div class="row" style="color: #00B9F1;" @click='showmassage(row)'>
      <span class="col-xs-2">户号：</span><span class="col-xs-4">{{row.f_userinfo_id}}</span>
      <span class="col-xs-2">姓名：</span><span class="col-xs-4">{{row.f_user_name}}</span>
    </div>
    <div class="row">
      <span class="col-xs-3">上期指数：</span><span class="col-xs-3">{{row.f_last_tablebase}}</span>
      <span class="col-xs-3">本期指数：</span><span class="col-xs-3">{{row.f_tablebase}}</span>
    </div>
    <div class="row">
      <span class="col-xs-3">用气量：</span><span class="col-xs-3">{{row.f_oughtamount}}</span>
      <span class="col-xs-2">电话：</span><span class="col-xs-4">{{row.f_user_phone}}</span>
    </div>
    <div class="row">
      <span class="col-xs-3">抄表状态：</span><span class="col-xs-3">{{row.f_meter_state}}</span>
      <span class="col-xs-3">缴费状态：</span><span class="col-xs-3">{{row.f_whether_pay}}</span>
    </div>
    <div class="row">
      <span class="col-xs-3">用气金额：</span><span class="col-xs-3">{{row.f_fee}}</span>
      <span class="col-xs-3">实际余额：</span><span class="col-xs-3">{{row.f_bal}}</span>
    </div>
    <div class="row">
      <span class="col-xs-3">抄表时间：</span><span class="col-xs-9">{{row.f_hand_date}}</span>
    </div>
    <div class="row">
      <span class="col-xs-3">地址：</span><span class="col-xs-9">{{row.f_address}}</span>
    </div>
  </div>
  <update-yichao :show='showmsg' :row ='showrow' v-on:close="flash" v-on:msg="flash"></update-yichao>
  </div>
</template>

<script>
import co from 'co'
import * as Util from '../../components/Util'
import appData from '../../components/appData'
import config from '../../components/config'
let handplansearch = function * (self) {
  let condition = `1=1 and  h.f_inputtor = '${appData.loginUser.logininfo[0].f_bieming}'`
  if(self.f_meter_chaobiao === '抄表'){
    condition +=  `and f_meter_state != '' and f_meter_state != '冲正'`
  }else if(self.f_meter_chaobiao === '已抄表'){
    condition +=  `and f_meter_state = '已抄表' `
  }else if(self.f_meter_chaobiao === '未抄表'){
    condition +=  `and f_meter_state = '未抄表' `
  }
  // let condition = `1=1 and  h.f_inputtor = '${appData.loginUser.logininfo[0].f_bieming}'`
  //if(self.model.f_user_name !=='' || self.model.f_userinfo_id !== '' || self.model.f_user_phone !==''){
  for (var key in self.model) {
    console.log(key);
    if (self.model[key] !== '' ) {
      if (key === 'f_start_date' || key === 'f_end_date' || key === 'f_user_name') {
        if (key === 'f_start_date') {
            condition  += ` and h.f_hand_date >= '${self.model['f_start_date']} 00:00:00' `
        } else if (key === 'f_end_date') {
          condition  += ` and h.f_hand_date <= '${self.model['f_end_date']} 23:59:59'`
        }else if(key === 'f_user_name'){
        condition  = condition + ` and h.${key} like '%${self.model[key]}%'`
        }
      } else {
          condition  = condition + ` and s.${key} = '${self.model[key]}'`
        }
      console.log(condition);
      }
    }
//  }
  // config.resultUrl +
  yield self.$resetpost(config.resultUrl + 'rs/sql/handplansearch', {data: {condition:condition}},
   {resolveMsg: null, rejectMsg: null})
  .then ((res)=> {
    self.user = res.data
    if(res.data.length === 0){
      self.$showMessage('没有此用户，请查看你输入的查询信息')
    }
  })
  .catch ((res)=> {
    self.$showMessage('获取用户数据失败，请查看你输入的查询信息')
  })

  yield self.$resetpost(config.resultUrl + 'rs/logic/realbalance',{data:{user:self.user}},
  {resolveMsg: null, rejectMsg: null})
  .then ((res)=> {
    self.users = res.data.users
  })
  .catch ((res)=> {
    self.$showMessage('获取用户数据失败，请查看你输入的查询信息')
  })

}

export default {
  title:'用户查询',
  props: ['f_meter_chaobiao'],
  data(){
    return{
      model:{
        f_userinfo_id: '',
        f_user_name: '',
        f_user_phone:'',
        f_start_date: Util.toStartDateString(),
        f_end_date: Util.toStandardDateString(),
        f_balance : ''
      },
      user: [],
      users: [],
      showmsg : false,
      showrow : ''
    }
  },
  methods: {
    search () {
      if (this.model.f_start_date !=='' || this.model.f_end_date !== '') {
        let gen = handplansearch(this)
        return co(gen)
      } else {
        this.$showMessage('请输入任意查询条件，确保信息准确性')
      }
    },
    showmassage(row){
      if (row.f_whether_pay === '是') {
        this.$showMessage('已缴费抄表记录不能更改')
      } else if (row.f_whether_pay === '否' && row.f_meter_state ==='已抄表') {
//跳转 已抄表未交费页面，修改抄表数据
        let condition = ` f_meter_state ='已抄表' and f_whether_pay = '否' and f_user_id = ${row.f_user_id} `
        this.$resetpost(config.resultUrl + 'rs/sql/singleTable_OrderBy',{data:{items:'MAX(f_hand_date) f_hand_date',tablename:'t_handplan',
        condition:condition,orderitem:'f_hand_date'}},
        {resolveMsg: null, rejectMsg: null})
        .then ((res)=> {
          row.f_max_date = res.data[0].f_hand_date
          if(res.data[0].f_hand_date > row.f_hand_date){
            this.$showMessage('非本期抄表不可更改')
          }else if(res.data[0].f_hand_date === row.f_hand_date){
            this.$showMessage('是否修改该用户的'+row.f_user_id+'表',['cancel','confirm'])
            .then((res)=>{
              if (res === 'confirm') {
                // 显示弹框
                this.showmsg = true
                this.showrow = row
              }
            })
          }
        })
        .catch ((res)=> {
          self.$showMessage('获取用户数据失败，请查看你输入的查询信息')
        })
      }else if (row.f_whether_pay === '否' && row.f_meter_state ==='未抄表') {
        let condition = ` f_meter_state ='未抄表' and f_whether_pay = '否' and f_user_id = ${row.f_user_id} `
        this.$resetpost(config.resultUrl + 'rs/sql/singleTable_OrderBy',{data:{items:'MAX(f_hand_date) f_hand_date',tablename:'t_handplan',
        condition:condition,orderitem:'f_hand_date'}},
        {resolveMsg: null, rejectMsg: null})
        .then ((res)=> {
          row.f_max_date = res.data[0].f_hand_date
          if(res.data[0].f_hand_date > row.f_hand_date){
            this.$showMessage('非本期抄表不可更改')
          }else if(res.data[0].f_hand_date === row.f_hand_date){
            this.$goto('state-huajia',{data:row},'self',this.callback)
          }
        })
        .catch ((res)=> {
          self.$showMessage('获取用户数据失败，请查看你输入的查询信息')
        })
      }
    },
    flash(){
      this.showmsg = false
      let gen = handplansearch(this)
      return co(gen)
    },
    callback(){
      let gen = handplansearch(this)
      return co(gen)
    }
  },
  ready : function () {
    let gen = handplansearch(this)
    return co(gen)
  }
}
</script>
